package com.jadesoft.stock.rp;

import java.util.Date;
import java.util.List;

import org.springframework.util.Assert;

import com.jadesoft.common.web.AjaxAction;
import com.jadesoft.stock.StockSubscriptBS;
import com.jadesoft.stock.StockSubscriptBSImpl;
import com.jadesoft.stock.entity.StockSubscript;
import com.jadesoft.stock.util.StockUtil;

public class StockSubscriptAction extends AjaxAction {
	private StockSubscriptBS subBs = new StockSubscriptBSImpl();

	/**
	 * 保存或更新股票订阅
	 */
	public StockSubscript save(StockSubscript sub){
		sub.setUserId(this.getUser().getId());
		String[] strs = StockUtil.splitStock(sub.getStock());
		sub.setStockName(strs[0]);
		sub.setStockSymbol(strs[1]);
		sub.setUpdateTime(new Date());
		return subBs.save(sub);
	}
	
	/**
	 * 删除订阅
	 */
	public void remove(StockSubscript sub){
		Assert.notNull(sub.getId(),"ID不能为空!");
		sub.setUserId(getUser().getId());
		subBs.remove(sub);
	}
	
	/**
	 * 获取用户的所有订阅
	 */
	public List<StockSubscript> list(){
		StockSubscript sub = new StockSubscript();
		sub.setUserId(getUser().getId());
		return subBs.list(sub);
	}

}
